package com.heima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dao.SetmealDao;
import com.heima.entity.PageResult;
import com.heima.entity.QueryPageBean;
import com.heima.pojo.Setmeal;
import com.heima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;
import com.heima.constant.RedisConstant;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        setCheckGroupAndSetmeal(setmeal, checkgroupIds);
        String filename = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,filename);
        generateMobileStaticHtml();
    }


    public void generateMobileStaticHtml(){
        List<Setmeal> list = setmealDao.findAll();
        generateSetmealListHtml(list);
        generateSetmealDetailHtml(list);
    }
    public void generateSetmealListHtml(List<Setmeal> list){
        Map map = new HashMap();
        map.put("setmealList",list);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    public void generateSetmealDetailHtml(List<Setmeal> list){
        for (Setmeal setmeal : list) {
            Map map = new HashMap();
            Setmeal setmeal1 = setmealDao.findById(setmeal.getId());
            map.put("setmeal",setmeal1);
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }





    public void generateHtml(String templateName,String htmlPageName,Map map){
       Configuration configuration = freeMarkerConfigurer.getConfiguration();
       /* Writer writer = null;*/
        OutputStreamWriter out =null;
        try {
            Template template = configuration.getTemplate(templateName);
            /* writer = new FileWriter(outPutPath+"/"+htmlPageName);*/
            out = new OutputStreamWriter(new FileOutputStream(outPutPath+"/"+htmlPageName),"UTF-8");
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





    private void setCheckGroupAndSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        if(checkgroupIds.length>0 && checkgroupIds!=null) {
            Integer setmealId = setmeal.getId();
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.setSetmealIdAndCheckGroupId(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return   new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }
}
